<?php
$title = "OnlineShop  - Register";
$css = "regedit.css";
include_once 'common/top.php';
include_once '../Class/DB.php';
include_once '../Class/User.php';
$userName=$_POST["name"];
$password=$_POST["password"];
$password2=$_POST["password2"];
$email=$_POST["email"];
$phone=$_POST["phone"];
$address=$_POST["address"];
$sex=$_POST["sex"];
$db=new DB();
$error_id=0;//error id
if(($phone=="")||strlen($phone)!=11)
{
	$error_id=6;
}
 if (ereg("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/",$email)){
 	$error_id=5;
}
if($email=="")
{
	$error_id=5;
}
if($password!=$password2)
{
	$error_id=4;
}
if($password2=="")
{
	$error_id=4;
}
if($password=="")
{
	$error_id=3;
}
if($userName!="")
{
if($db->Is_exist($userName))
{
	$error_id=2;
}
}else{
	$error_id=1;
}

if (!isset($_POST["name"])){
	$error_id=-999;
}

if($error_id==0)
{
	$A_user=new User();
	$A_user->userName=$userName;
	$A_user->password=$password;
	$A_user->phone=$phone;
	$A_user->sex=$sex=="male"?0:1;
	$A_user->level=0;
	$A_user->exp=0;
	$A_user->email=$email;
	$A_user->deliver_addr=$address;
	$A_user->insert_self();
    //header("location:regedit_success.php");
    $_SESSION["Passed"]=true;
    $_SESSION['User']=$db->get_userName_info($userName);	
    ?>
<script language="JavaScript">
window.location.href="regedit_success.php";
</script>
<?php	
}
?>


<div id="regedit">
<form action="<?PHP $_SERVER['PHP_SELF']?>" method="post">
<table>
	<tr>
		<th class="t1">
		</th>
		<th class="t2">
		</th>
		<th class="t3">
		</th>
	</tr>
	<tr>
		<td>
		<b>UserName:</b>
		</td>
		<td><input type="text" name="name" value="<?php echo $userName;?>" autocomplete="off" /></td>
		<?php if($error_id==1){?>
		<td>
			<p>Invalid user name!</p>
		</td>
		<?php 	}?>
		<?php if($error_id==2){?>
		<td>
			<p>Name already existed!</p>
		</td>
		<?php 	}?>
		
	</tr>
	<tr>
		<td>
		<b>Password:</b>
		</td>
		<td><input type="password" name="password" value="<?php echo $password;?>"/></td>
		<?php if($error_id==3){?>
		<td>
			<p>Invalid Password!</p>
		</td>
		<?php 	}?>
	</tr>
	<tr>
		<td>
		<b>Confirm Password:</b>
		</td>
		<td><input type="password" name="password2" value="<?php echo $password2;?>"/></td>
		<?php if($error_id==4){?>
		<td>
			<p>Please Confirm again!</p>
		</td>
		<?php 	}?>
	</tr>
	<tr>
		<td>
		<b>Sex:</b>
		</td>
		<td>
			<input type="radio" name="sex" value="male" checked="checked"/>
			<label>male</label>
			<input type="radio" name="sex" value="female" />
			<label>female</label>
		</td>
	</tr>
	<tr>
		<td>
		<b>Email:</b>
		</td>
		<td><input type="text" name="email" value="<?php echo $email;?>"/></td>
			<?php if($error_id==5){?>
		<td>
			<p>Invalid E-mail Address!</p>
		</td>
		<?php 	}?>
	</tr>
	<tr>
		<td>
		<b>phone:</b>
		</td>
		<td><input type="text" name="phone" value="<?php echo $phone;?>"/></td>
			<?php if($error_id==6){?>
		<td>
			<p>Invalid phone number!</p>
		</td>
		<?php 	}?>
	</tr>
	<tr>
		<td>
		<b>Address:</b>
		</td>
		<td><input type="text" name="address" value="<?php echo $address;?>"/></td>
	</tr>
	<tr>
		<td>
		
		</td>
		<td>
			<p class="no"><a href=""><i>Online Shopping Mall Agreement</i></a></p>
		</td>
		<td>
			
		</td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" value="" /></td>
		<td></td>
	</tr>
</table>

</form>
</div>
<?php
include_once 'common/tail.php';
?>